import Server from "@/config/server";
import request from '@/utils/request';
import { showErrors, showError, showSuccess } from '@/utils/showMsg';

const state = {
  requestCode : false,
  dataLists: [],
  Fields: [],
  deviceCodeData: []
};
const actions = {
  //列表
  getLists({commit}, page) {
    request.get(Server.action.equipmentCode_list, { params: page })
    .then(response => {
      console.log(response);
        if (response.data.errorCode === 0) {
            commit("setLists", response.data.result.list);
            let fields = response.data.result.fields;
            fields.push({
                title: '操作',
                align: 'center',
                width: 120,
                scopedSlots: { customRender: 'action' },
                fixed: 'right',
            })
            fields[0].width = 60
            fields.forEach(function (item, i) {
              item.dataIndex = item.key
              item.align = 'center';
              if(i < fields.length-2) {
                item.width = item.width || 120
              }
              if(item.key === "is_OnLine") {
                item.scopedSlots = { customRender: 'is_OnLine' }
              }
            })
            commit("setFields", fields);
        } else {

        }
    })
  },
  //添加
  dataAdd({commit}, page) {
    commit("setErrorCode", false);
    request.post(Server.action.equipmentCode_add, page )
    .then(response => {
      console.log(response);
      commit("setErrorCode", response.data.errorCode);
      if (response.data.errorCode === 0) {
        showSuccess(response.data.result)
      } else if(response.status === 422) {
        showErrors( response.data.errors )
      } else {
        showError( response.data.message )
      }
    })
  },
  //编辑
  dataEdit({commit}, page) {
    commit("setErrorCode", false);
    request.post(Server.action.equipmentCode_update, page )
    .then(response => {
      console.log(response);
      commit("setErrorCode", response.data.errorCode);
      if (response.data.errorCode === 0) {
        showSuccess(response.data.result)
      } else {
        showError( response.data.message )
      }
    })
  },
  // ID删除 > 参数为：url/id
  dataDel({commit}, id) {
    request.get(Server.action.equipmentCode_delete + id)
    .then(response => {
      console.log(response);
      if (response.data.errorCode === 0) {
        showSuccess(response.data.result)
      } else {
        showError( response.data.message )
      }
    })
  },
  // 设备编码未使用码
  deviceCodeAll({commit}, id) {
    request.get(Server.action.equipmentCode_all, { params: {equipment_id: id} })
    .then(response => {
      console.log(response);
      if (response.data.errorCode === 0) {
        commit("setDeviceCodeAll", response.data.result);
      } else {
        showError( response.data.message )
      }
    })
  },
  //设备码状态更改
  switchState({commit}, page) {
    commit("setErrorCode", false);
    request.post(Server.action.handingStatus, page )
    .then(response => {
      console.log(response);
      commit("setErrorCode", response.data.errorCode);
      if (response.data.errorCode === 0) {
        showSuccess(response.data.result)
      } else {
        showError( response.data.message )
      }
    })
  },
};
const mutations = {
  setErrorCode(state, text) {
	    state.requestCode = text;
	},
	setLists(state, lists) {
	    state.dataLists = lists;
	},
  setFields(state, lists) {
	    state.Fields = lists;
	},
  setDeviceCodeAll(state, data) {
    state.deviceCodeData = data;
  },
};
export default {
    namespaced: true,
    state,
    actions,
    mutations
};
